package com.mutual.commom.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLDecoder;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;


public class SystemUtil {
	public static String getTime(){
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		return sf.format(new Date()).toString();
	}
	public static Date getSyear(String syear){
		Date currentTime=SystemUtil.getDate();
		SimpleDateFormat sf=new SimpleDateFormat("MM-dd hh:mm:ss");
		String str=syear+"-"+sf.format(currentTime);
		DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date yearTime=null;
		try {
			yearTime=dateFormat.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return yearTime;
	}
	public static String getFormatDate(Date date){
		SimpleDateFormat sf=new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sf.format(date);
	}
	
	public static String getFormatDateOver(Date date){
		SimpleDateFormat sf=new  SimpleDateFormat("yyyy-MM-dd");
		return sf.format(date);
	}
	
	public static String getFormatDateOther(Date date){
		SimpleDateFormat sf=new  SimpleDateFormat("yyyy-MM-");
		return sf.format(date);
	}
	public static String getDay(){
		SimpleDateFormat dateformat1=new SimpleDateFormat("dd");
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(new Date());
		return dateformat1.format(calendar.getTime());
	}
	public static String getOrderNum(){
		SimpleDateFormat dateformat1=new SimpleDateFormat("yyyyMMdd");
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(new Date());
		return dateformat1.format(calendar.getTime())+getRandom(4);
	}
	
	public static String getRandom(int count) {
		StringBuffer sb = new StringBuffer();
		String str = "0123456789";
		Random r = new Random();
		for (int i = 0; i < count; i++) {
			int num = r.nextInt(str.length());
			sb.append(str.charAt(num));
			str = str.replace((str.charAt(num) + ""), "");
		}
		return sb.toString();
	}
	
	public static String getYear(){
		SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy");
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(new Date());
		return dateformat1.format(calendar.getTime());
	}
	public static Date getDateByString(String str){
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d=null;;
		try {
			d = sf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}
	public static Date getYMDHMByString(String str){
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date d=null;;
		try {
			d = sf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}
	public static String getMonth(){
		SimpleDateFormat dateformat1=new SimpleDateFormat("MM");
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(new Date());
		return dateformat1.format(calendar.getTime());
	}
	public static Date getDate(){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.getTime();
	}
	public static String getString(Object o){
		String strValue=null;
		try {
			if(o==null||o.equals("")){
				return "";
			}
			strValue=new String(o.toString().getBytes("iso-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			
		}
		return strValue;
	}
	public static String getFloat(Float f){
		DecimalFormat format=new DecimalFormat("#.00");
		return format.format(f);
	}
	//encode 转码
	public static String encodeToString(String str,String encode){
		try {
			String strDencode = URLDecoder.decode(str,encode);
			return strDencode;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}
	/**
	 * Base64算法封装
	 * @param str
	 * @return
	 */
//	public static String encode(Object o){
//		return new String(Base64.encodeBase64(String.valueOf(o).getBytes()));
//	}
//	
//	public static String decode(Object o){
//		return new String(Base64.decodeBase64(String.valueOf(o).getBytes()));
//	}
	
	public static long getYearMonthDay(){
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		String date=sf.format(new Date());
		Date d=null;
		try {
			d=sf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d.getTime();
	}
	
	public static Date getYearMonthDay(String date){
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		Date d=null;;
		try {
			d = sf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}
	
	public static String getYMDStr(String str){
		return getFormatDateOver(getYMDHMByString(str));
	}
	
	public static String getHM(Date date){
		SimpleDateFormat sf=new SimpleDateFormat("HH:mm");
		return sf.format(date);
	}
	
	public static int getWeekByDateString(String dateStr){
		Date date=getYearMonthDay(dateStr);
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		boolean isFirstSunday = (calendar.getFirstDayOfWeek() == Calendar.SUNDAY);
		//获取周几
		int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
		//若一周第一天为星期天，则-1
		if(isFirstSunday){
		  weekDay = weekDay - 1;
		  if(weekDay == 0){
		    weekDay = 7;
		  }
		}
		return weekDay;
	}
	
	/**
	 * 四舍五入
	 * @param num
	 * @param scale
	 * @return
	 */
	public static double round(double num,int scale){
		return new BigDecimal(num).divide(new BigDecimal(1), scale, RoundingMode.HALF_UP).doubleValue();
	}
	
	
	public static	double getDoubleData(Object obj){
		return Double.parseDouble(obj.toString());
	}
	
	public static boolean isNull(Object o){
		return o==null||o.toString().equals("")?true:false;
	}
	
	public static int getInt(Object o){
		if(isNull(o)){
			return 0;
		}
		return Integer.parseInt(o.toString());
	}
	
	public static int getPageNo(Object o){
		if(o==null||"".equals(o)){
			return 1;
		}
		return Integer.parseInt(o.toString());
	}
	
	public static boolean isNull(Integer integer){
		return integer==null||integer==0;
	}
	
	public static boolean isEquals(Integer i,Integer j){
		int a=(i==null?0:i.intValue());
		int b=(j==null?0:j.intValue());
		return (a==b);
	}
}
